//
//  FirstViewController.m
//  UMDReader
//
//  Created by 张 霄鹤 on 10-1-6.
//  Copyright xhzhang 2010. All rights reserved.
//

#import "FirstViewController.h"

#import "UMDFileReader.h"
#import "Chapter.h"

#import "DataManager.h"


@implementation FirstViewController


/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//UMDFileReader *reader = [[[UMDFileReader alloc] init] autorelease];
//	firstBook = [reader read:@"test1"];
//	if(firstBook){
//		lblTitle.text = firstBook.bookTitle;
//		Chapter *ch = [firstBook.bookChapters objectAtIndex:0];
//		if(ch){
//			txtContent.text = ch.content;
//		}
//	}
	DataManager *dm = [DataManager sharedDataManager];
	// [dm lookupFiles:@"umd"];
	// firstBook = [[dm loadEBook:@"res://mslz_1.umd"] retain];
	ebfile = [[dm loadEBook:@"res://mslz.ebf"] retain];
	self.navigationItem.title = [ebfile getTitle];
	// cv = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
	//if(firstBook){
//		lblTitle.text = firstBook.bookTitle;
//		Chapter *ch = [firstBook.bookChapters objectAtIndex:0];
//		if(ch){
//			txtContent.text = ch.content;
//		}
//	}
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSInteger ret = 0;
	if(ebfile){
		ret = [ebfile.chapters count];
	}
    return ret;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	NSInteger row = indexPath.row;
	
	Chapter *ch = (Chapter *)[ebfile.chapters objectAtIndex:row];
	
	cell.textLabel.text = ch.title;
	
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
	
	NSInteger row = indexPath.row;
	
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	
	// Chapter *ch = (Chapter *)[firstBook.bookChapters objectAtIndex:row];
	EBChapter *ch = (EBChapter *)[ebfile.chapters objectAtIndex:row];
	if(cv == nil){
		cv = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
	}
	[cv setEBChapter:ch];
	[self.navigationController pushViewController:cv animated:NO];
	//[cv setContent:ch.content withTitle:ch.title];
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


- (void)dealloc {
	[firstBook release];
	[cv release];
    [super dealloc];
}

@end
